//基于useRoutes测试
import { createRef, lazy } from "react";
import { Navigate } from "react-router-dom";

import Login from "@/views/login/Login";
import Error_404 from "@/views/error-page/index";
import LayoutIndex from "@/layout";
import { SuspenseComponent } from "@/utils/MyFun";

import HomeRoutes from "@/views/home/route";
import ArticleRoputes from "@/views/article/route";
import AuthRoutes from "@/views/auth/route";
import ErrorRoutes from "@/views/error-page/route";
import LogRoutes from "@/views/log/route";
import MessageRoutes from "@/views/message/route";
import ProfileRoutes from "@/views/profile/route";
import SettingRoutes from "@/views/setting/route";
import userRoutes from "@/views/user/route";

//懒加载的组件
// const Home = lazy(() => import("@/views/home"));

//获取一个目录下以route.js结尾导出文件，返回的是一个函数,true代表查询子目录
let routeModules = require.context("@/views", true, /\/route\.js$/);
// routeModules.keys()获取以route.js结尾文件导出的文件目录
let asyncRoutes = [];

asyncRoutes = routeModules.keys().map((key) => {
  //key是路径， routeModules(key)是导出的模块
  const module = routeModules(key); //调用函数传入文件目录src，获取对应导出的对象
  return module.default;
});

let react_Modules = require.context("../views", true, /\/index\.jsx$/);

// 获取views目录下所有的index文件导出的模块
let reactModules = react_Modules.keys().map((key) => {
  // reactModules.keys()是获取所有index文件的路径组成的数组
  //key是文件的路径
  const module = react_Modules(key);
  return module.default;
});

/**
 * 传入需要查找的路径，从所有模块中返回对应的导出模块
 * @param {*} comparePath
 * @returns
 */
const getEqualReactModules = (comparePath) => {
  //获取所有的index.jsx的文件路径遍历
  for (let i = 0; i < react_Modules.keys().length; i++) {
    let path = react_Modules.keys()[i]; //路径
    //传入的路径进行比较
    if (comparePath.includes(path.slice(1))) {
      //slice从第一个字符串开始截取是因为path的第一个字符是./起头，因此需要去除
      //找到对应的模块
      return react_Modules(path).default;
    }
  }
};

export { asyncRoutes, reactModules, getEqualReactModules };

//创建路由表
let routes = [
  {
    path: "/",
    element: <Navigate to="/home" />,
    nodeRef: createRef(null),
    redirect: true,
  },
  {
    name: "Login",
    path: "/login",
    element: <Login />,
    nodeRef: createRef(null),
    isHidden: true,
    meta: {
      title: "登录页",
    },
  },
  {
    name: "404",
    path: "/404",
    element: <Error_404 />,
    nodeRef: createRef(null),
    isHidden: true,
    meta: {
      title: "错误页",
    },
  },
  {
    name: "NotFound",
    path: "*",
    element: <Navigate to="/404" />,
    isHidden: true,
    nodeRef: createRef(null),
  },
  {
    path: "/",
    element: <LayoutIndex />,
    nodeRef: createRef(null),
    children: [
      // HomeRoutes,
      // ArticleRoputes,
      // AuthRoutes,
      // // ErrorRoutes,
      // LogRoutes,
      // MessageRoutes,
      // ProfileRoutes,
      // SettingRoutes,
      // userRoutes,
      //#region
      // {
      //   path: "/home",
      //   // element: SuspenseComponent(<Home />),
      //测试的根据后端返回文件路径引入异步懒加载组件，这里不要写../views
      //   element: SuspenseComponent("/home/index.jsx"),
      //   nodeRef: createRef(null),
      //   meta: {
      //     title: "首页",
      //     icon: "ic:sharp-home",
      //   },
      // },
      // {
      //   path: "/article",
      //   element: <Outlet />, //子路由存放在这里
      //   nodeRef: createRef(null),
      //   meta: {
      //     title: "文章管理",
      //     icon: "ic:twotone-article",
      //     order: 2,
      //     // role: ['admin'],
      //     // requireAuth: true,
      //   },
      //   children: [
      //     {
      //       path: "/article",
      //       element: <Navigate to="/article/list" />,
      //       nodeRef: createRef(null),
      //     },
      //     {
      //       path: "/article/list",
      //       element: SuspenseComponent(<ArtList />),
      //       nodeRef: createRef(null),
      //       meta: {
      //         title: "文章列表",
      //         icon: "material-symbols:format-list-bulleted",
      //         // role: ['admin'],
      //         // requireAuth: true,
      //         keepAlive: true,
      //       },
      //     },
      //     {
      //       path: "/article/write",
      //       element: SuspenseComponent(<ArtWrite />),
      //       nodeRef: createRef(null),
      //       meta: {
      //         title: "发布文章",
      //         icon: "icon-park-outline:write",
      //         // role: ['admin'],
      //         // requireAuth: true,
      //         keepAlive: true,
      //       },
      //     },
      //     {
      //       path: "/article/write/:id",
      //       element: SuspenseComponent(<ArtWrite />),
      //       nodeRef: createRef(null),
      //       meta: {
      //         title: "编辑文章",
      //         icon: "icon-park-outline:write",
      //         // role: ['admin'],
      //         // requireAuth: true,
      //         // keepAlive: true,
      //       },
      //     },
      //     {
      //       path: "/article/category",
      //       element: SuspenseComponent(<ArtCategory />),
      //       nodeRef: createRef(null),
      //       meta: {
      //         title: "分类管理",
      //         icon: "tabler:category",
      //         // role: ['admin'],
      //         // requireAuth: true,
      //         keepAlive: true,
      //       },
      //     },
      //     {
      //       path: "/article/tag",
      //       element: SuspenseComponent(<ArtTag />),
      //       nodeRef: createRef(null),
      //       meta: {
      //         title: "标签管理",
      //         icon: "tabler:tag",
      //         keepAlive: true,
      //       },
      //     },
      //   ],
      // },
      //#endregion
    ],
  },
];

export default routes;

export const NOT_FOUND_ROUTE = {
  name: "NotFound",
  path: "*",
  element: <Navigate to="/404" />,
  isHidden: true,
  nodeRef: createRef(null),
};

export const EMPTY_ROUTE = {
  name: "Empty",
  path: "*",
  element: null,
  nodeRef: createRef(null),
};
